import {
	defineStore
} from "pinia";

// 定义用户存储
export const useUserStore = defineStore("user", {
	state: () => ({
		userInfo: {
			openid: uni.getStorageSync("openid") || "",
			avatar: "",
			nickname: "",
			phonenumber: "",
			isItemOperator: false,
			isAdmin: true,
		},
		//缓存项目id 因为会与isItemOperator信息同步使用 就放在这了
		itemId: null,
		itemName: null,
	}),
	actions: {
		setUserInfo(item) {
			this.userInfo.nickname = item.userName;
			this.userInfo.phonenumber = item.phonenumber;
			if (item.avatar && item.avatar.includes("https://dn.xinnuanyun.com")) {
				this.userInfo.avatar = item.avatar;
			} else {
				this.userInfo.avatar = "";
			}
		},
		getUserInfo() {
			return this.userInfo;
		},
		setUserName(item) {
			this.userInfo.nickname = item;
		},
		setUserAvatar(item) {
			if (item && item.includes("https://dn.xinnuanyun.com")) {
				this.userInfo.avatar = item;
			} else {
				this.userInfo.avatar = "";
			}
		},
		setItemId(id) {
			this.itemId = id;
		},
		getItemId() {
			return this.itemId || null;
		},
		setItemName(name) {
			this.itemName = name;
		},
		getItemName() {
			return this.itemName || null;
		},
		updAdminType(type) {
			this.userInfo.isAdmin = type;
		},
	},
});